Plotly image.png

  • Plotly is an interactive data visualization library that allows you to create web-based, interactive visualizations and dashboards. It supports a wide range of chart types and is often used for creating dynamic and interactive plots.

Modules in Plotly¶

  1. plotly.graph_objects : It module contains the objects (Figure, layout, data, and the definition of the plots like scatter plot, line chart) that are responsible for creating the plots.
  2. plotly.express : plotly.express module can create the entire Figure at once. It uses the graph_objects internally and returns the graph_objects.Figure instance.
  • Note: In summary, if you need fine-grained control and customization, or if you are creating complex visualizations, plotly.graph_objects might be more suitable. If you want a quick and easy way to create common plots with less code, plotly.express is a convenient choice
In [1]:
pip install plotly
Requirement already satisfied: plotly in c:\users\asus\anaconda3\lib\site-packages (5.9.0)
Requirement already satisfied: tenacity>=6.2.0 in c:\users\asus\anaconda3\lib\site-packages (from plotly) (8.0.1)
Note: you may need to restart the kernel to use updated packages.
In [1]:
#plotly.graph_objects
import plotly.graph_objects as go
In [2]:
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1,2,3,4,5],y=[3,5,6,7,8],mode='markers'))
In [3]:
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1,2,3,4,5],y=[3,5,6,7,8],mode='lines'))
In [4]:
fig = go.Figure()
fig.add_trace(go.Bar(x=[1,2,3,4,5],y=[3,5,6,7,8]))
fig.show()
In [10]:
x1 = [1,2,2,2,3,3,4,1,5,6,8,1,9,10,12,13]
fig = go.Figure(data=[go.Histogram(x=x1)])
fig.show()
In [7]:
x1
Out[7]:
[1, 2, 2, 2, 3, 3, 4, 1, 5, 6, 8, 1, 9, 10, 12, 13]
In [11]:
x = [1,2,3,3,4,4,5,5,6,6,7,78,9,9,0,8,9,8,7,6,5,1,2,2,23,34,45,5]
fig = go.Figure(data=[go.Histogram(x=x)])
fig.show()
In [12]:
import seaborn as sns
In [15]:
df = sns.load_dataset('tips')
In [16]:
df
Out[16]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
... ... ... ... ... ... ... ...
239 29.03 5.92 Male No Sat Dinner 3
240 27.18 2.00 Female Yes Sat Dinner 2
241 22.67 2.00 Male Yes Sat Dinner 2
242 17.82 1.75 Male No Sat Dinner 2
243 18.78 3.00 Female No Thur Dinner 2

244 rows × 7 columns

In [19]:
fig= go.Figure(data=[go.Histogram(x=df.total_bill)])
fig.show()
In [20]:
fig = go.Figure()
fig.add_trace(go.Scatter(x=df.total_bill,y=df.tip,mode='markers'))
In [22]:
fig = go.Figure()
fig.add_trace(go.Scatter3d(x=[1,2,3,4,5],y=[3,5,6,7,8],mode='markers',z=[2,3,4,5,6]))
fig.show()
In [23]:
#plotly.express

import plotly.express as px
In [24]:
fig = px.line(x=[1,2,3],y=[1,2,3])
fig.show()
In [26]:
df = px.data.iris()
In [27]:
df
Out[27]:
sepal_length sepal_width petal_length petal_width species species_id
0 5.1 3.5 1.4 0.2 setosa 1
1 4.9 3.0 1.4 0.2 setosa 1
2 4.7 3.2 1.3 0.2 setosa 1
3 4.6 3.1 1.5 0.2 setosa 1
4 5.0 3.6 1.4 0.2 setosa 1
... ... ... ... ... ... ...
145 6.7 3.0 5.2 2.3 virginica 3
146 6.3 2.5 5.0 1.9 virginica 3
147 6.5 3.0 5.2 2.0 virginica 3
148 6.2 3.4 5.4 2.3 virginica 3
149 5.9 3.0 5.1 1.8 virginica 3

150 rows × 6 columns

In [29]:
fig=px.line(df , x='species',y='petal_width')
fig.show()
In [30]:
fig=px.bar(df , x='sepal_length',y='petal_width')
fig.show()
In [31]:
fig=px.histogram(df , x='sepal_length',y='petal_width')
fig.show()
In [32]:
fig=px.scatter(df , x='sepal_length',y='petal_width')
fig.show()
In [36]:
fig=px.scatter(df , x='species',y='petal_width',size='petal_length',color='species')
fig.show()
In [37]:
df = px.data.tips()
In [38]:
df
Out[38]:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
... ... ... ... ... ... ... ...
239 29.03 5.92 Male No Sat Dinner 3
240 27.18 2.00 Female Yes Sat Dinner 2
241 22.67 2.00 Male Yes Sat Dinner 2
242 17.82 1.75 Male No Sat Dinner 2
243 18.78 3.00 Female No Thur Dinner 2

244 rows × 7 columns

In [39]:
fig=px.pie(df , values='total_bill', names='day')
fig.show()
In [40]:
fig=px.box(df , x='day', y='total_bill')
fig.show()
In [41]:
fig=px.violin(df , x='day', y='total_bill')
fig.show()
In [ ]: